package nc.gk.gkxm.exception;

import lombok.extern.slf4j.Slf4j;
import nc.gk.gkxm.utils.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器，处理项目中抛出的业务异常
 */
//@ControllerAdvice
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 捕获业务异常
     * @param ex
     * @return
     */
    @ExceptionHandler
    public Result exceptionHandler(BaseException ex){
        log.error("异常信息：{}", ex.getMessage());
        return Result.fall(400,ex.getMessage());
    }
    @ExceptionHandler
    public Result exceptionHandler(Exception ex){
        String message = ex.getMessage();
        if (message.contains("Duplicate entry")){
            String[] split = message.split(" ");
            String username = split[2];
            String msg = username+ "账号已存在";
            return Result.fall(400,msg);
        } else {
            return Result.fall(400,message);
        }
    }

}